<?php

namespace XiaoZhuBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use XiaoZhuBundle\Helper\MyController;
use AdminsBundle\Entity\Product;
/**
 * @Route("/cart")
 */
class CartController extends MyController
{
    /**
     * @Route("/info", name="cartInfo")
     */
    public function cartInfo(Request $request)
    {
        if (!$product = $request->cookies->get("cart")) {$product=array();}
        $lists = array();
        $total = 0;
        $count= 0;
        if (is_array($product) && !empty($product)) {
            foreach ($product as $pid => $info) {
                $num = $info["num"];
                $rep = $this->DB("AdminsBundle:Product");
                $productBasic = $rep->findOneBy(array("id"=>$pid));
                if (!$productBasic) {
                    continue;
                }
                $rep = $this->DB("AdminsBundle:ProductShow");
                $productShow = $rep->findOneBy(array("id"=>$pid));
                $lists[] = array(
                    "basic"   => $productBasic ,
                    "show"   => $productShow,
                    "num"   => $num,
                    "total"     => $num * $productBasic->getPrice()
                );
                $total +=  $num * $productBasic->getPrice();
                $count += $num;
            }
        }
        return $this->render('@XiaoZhu/cart.html.twig',array("lists"=>$lists,"total"=> $total,"num"=>$count));
    }

}
